package com.webchat.pgc.messagequeue.consumer.redis;

import com.webchat.pgc.messagequeue.service.LocalCacheRefreshService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LocalCacheRefreshListener implements MessageListener {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LocalCacheRefreshService localCacheRefreshService;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = (String) redisTemplate.getStringSerializer().deserialize(message.getChannel());
        String messageStr = (String) redisTemplate.getValueSerializer().deserialize(message.getBody());
        log.info("LocalCacheRefreshListener.onMessage =====> channel:{} messageStr:{}", channel, messageStr);
        // 加入任务队列（遇到通知大流量场景，缓解consumer压力）
        localCacheRefreshService.consume(messageStr);
    }
}
